<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>官网聚合demo</title>
    <link rel="stylesheet" href="../css/ol.css" type="text/css">
    <!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
    <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
    <script src="../build/ol.js"></script>
</head>
<body>
<div id="map" class="map"></div>
<form>
    <label>cluster distance</label>
    <input id="distance" type="range" min="0" max="100" step="1" value="40"/>
</form>
<script>
    var distance = document.getElementById('distance');

    /*var count = 20000;
    var features = new Array(count);
    var e = 4500000;
    for (var i = 0; i < count; ++i) {
        var coordinates = [2 * e * Math.random() - e, 2 * e * Math.random() - e];
        features[i] = new ol.Feature(new ol.geom.Point(coordinates,'XY'));
    }*/

    var features = [];
    var i = 0;
    var fbl = parseFloat(0.05);
    var startLon = parseFloat(105);
    var startLat = parseFloat(28);
    var endLon = parseFloat(111.35);
    var endLat = parseFloat(34.35);
    for(var lon=startLon; lon<=endLon; lon+=fbl){
        for(var lat=startLat; lat<=endLat; lat+=fbl){
            var feature = new ol.Feature(new ol.geom.Point([parseFloat(lon),parseFloat(lat)]));
            features.push(feature);
            feature.set('value',lon);
            i++;
        }
    }

    var source = new ol.source.Vector({//提供矢量层的特性源
        features: features //特点特征[包含数据和数据类型等等]
    });

    var clusterSource = new ol.source.Cluster({//聚集矢量数据
        //distance: parseInt(distance.value, 10),//间距，以10为基数解析；间距越小，合并的越少，显示的越多
        distance:75,//聚合的像素距离，默认是20
        source: source//矢量数据的矢量图，为渲染优化
    });

    var styleCache = {};
    var clusters = new ol.layer.Vector({//呈现客户端的矢量数据
        source: clusterSource,
        style: function(feature) {//图层样式
            /**图层放大或缩小或点击移动都会执行，有多少个需要显示的数量(合并后的数量)就执行多少次*/
            var size = feature.get('features').length;//feature.get('features')浏览器目前显示的某个合并的数据集合，size显示的值，多少个数据合并而成（ol.source.Cluster聚集才能这么获取）
            var style = styleCache[size];//相同的数值，显示同一种样式
            if (!style) {
                style = new ol.style.Style({
                    image: new ol.style.Circle({//圆
                        radius: 10,//半径
                        stroke: new ol.style.Stroke({//笔画
                            color: '#fff'//颜色白色
                        }),
                        fill: new ol.style.Fill({
                            color: '#3399CC'//填充蓝色
                        })
                    }),
                    text: new ol.style.Text({//文本
                        text: size.toString(),
                        fill: new ol.style.Fill({
                            color: '#fff'//填充白色
                        })
                    })
                });
                styleCache[size] = style;
            }
            return style;
        }
    });

    var raster = new ol.layer.Tile({//设置标题
        source: new ol.source.OSM()//公开街道地图层源
    });

    var map = new ol.Map({
        layers: [raster, clusters],
        target: 'map',
        view: new ol.View({
            center: [0, 0],
            zoom: 2
        })
    });

    distance.addEventListener('input', function() {
        clusterSource.setDistance(parseInt(distance.value, 10));
    });

</script>
</body>
</html>